# -*- coding: utf-8 -*- 
import random

def get_plugin(_silent=True):
    name = request.args(0) or None
    
    if not name:
        raise HTTP(404)
    
    plugin = db(db.plugin.name == name).select().first()
    
    return plugin

### required - do no delete
def user(): return dict(form=auth())
def download(): return response.download(request,db)
#=URL('download', args=image.file)}
def call():
    session.forget()
    return service()
### end requires

def error():
    return dict()

def index():
    plugins = db(db.plugin.id > 0).select(orderby=db.plugin.modified_on)
    
    total = db(db.featured.id > 0).count()
    
    if total:
        ridx = random.randrange(0, total)
        
        featured_plugin_id = db(db.featured.id > 0).select(limitby=(ridx, 1)).first()
        featured_plugin = db.plugin[featured_plugin_id.id_plugin]
    else:
        featured_plugin = None
        
    return dict(plugins=plugins,
                featured_plugin=featured_plugin)
    
def plugin():
    plugin = get_plugin()
    
    return dict(plugin=plugin)

@auth.requires_membership('Admin')
def admin():
    plugins = db(db.plugin.demo_approved == False).select()
    
    return dict(plugins=plugins)

@auth.requires_membership('Admin')
def approve_demo():
    plugin = get_plugin()
    
    Plugin.install(plugin)
    
    plugin.demo_approved = True
    plugin.update_record()
    
    session.flash = "Plugin demo application approved and installed"
    
    return redirect(request.env.http_referer)

@auth.requires_membership('Admin')
def make_featured():
    plugin = get_plugin()
    
    db.featured.insert(id_plugin = plugin.id)
        
    return redirect(request.env.http_referer)

@auth.requires_membership('Admin')
def remove_featured():
    plugin = get_plugin()
    
    db(db.featured.id_plugin == plugin.id).delete()
    
    return redirect(request.env.http_referer)
    
@auth.requires_login()
def user_list():
    my_plugins = db(db.plugin.created_by == auth.user.id) \
                    .select(orderby=db.plugin.created_on)
    
    return dict(plugins=my_plugins)

@auth.requires_login()
def plugin_create():
    form = crud.create(db.plugin, next=URL('user_list'))
    
    return dict(form=form)

def on_update_accept(id_plugin):
    def _on_update_accept(form, id_plugin=id_plugin):
        plugin = db.plugin[id_plugin]
        
        plugin.demo_approved = False
    return _on_update_accept    

@auth.requires_login()
def plugin_update():
    plugin = get_plugin()
    
    if auth.user.id != plugin.created_by.id:
        raise HTTP(403)
    
    form = crud.update(db.plugin, plugin, next=URL('user_list'),
                       onaccept=on_update_accept(plugin.id))
    
    return dict(form=form)

@auth.requires_login()
def tplugin_create():
    form=crud.create(db.t_plugin,next='plugin_read/[id]')
    return dict(form=form)

@auth.requires_login()
def tplugin_read():
    record = db.t_plugin(request.args(0)) or redirect(URL('error'))
    form=crud.read(db.t_plugin,record)
    return dict(form=form)

@auth.requires_login()
def tplugin_update():
    record = db.t_plugin(request.args(0)) or redirect(URL('error'))
    form=crud.update(db.t_plugin,record,next='plugin_read/[id]')
    return dict(form=form)

@auth.requires_login()
def tplugin_select():
    f,v=request.args(0),request.args(1)
    query=f and db.t_plugin[f]==v or None
    rows=crud.select(db.t_plugin,query=query)
    return dict(rows=rows)

@auth.requires_login()
def tplugin_search():
    form, rows=crud.search(db.t_plugin)
    return dict(form=form, rows=rows)

